/****************************************************************************
  @file    : tvbox_89.c
  @date    : 2021-1-7
  @brief   : wandeng
*****************************************************************************/
/* Includes ----------------------------------------------------------------*/
#include "remote.h"
#include "string.h"
#include "tvbox.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//ʱ�䶨��

#define  BOX_ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)300  )
#define  BOX_ENDCODE_TIME_L	    (uint16_t)( (~0x8000)& (uint16_t)10000)
#define  BOX_BIT0_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)755  )
#define  BOX_BIT0_TIME_H			(uint16_t)( 0x8000   | (uint16_t)330  )
#define  BOX_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)330  )
#define  BOX_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)1800 )

//��ֵ����
static const uint8_t BOX_Code[TVBOX_KEY_NUM] = {
 0x00 ,//0x01����չ-���أ�
 0x00 ,//0x02����չ-AV/TV��
 0x00 ,//0x03����չ-����+��
 0x00 ,//0x04����չ-����-��
 0x00 ,//0x05����չ-Ƶ��+��
 0x00 ,//0x06����չ-Ƶ��-��
 0x00 ,//0x07����չ-������
 0x00 ,//0x08����չ-������
 0x00 ,//0x09����չ-������
 0x00 ,//0x0A����չ-������
 0x16 ,//0x0B�����أ�
 0x29 ,//0x0C���˵���
 0x00 ,//0x0D����Ϣ��
 0xE4 ,//0x0E�����أ�
 0x00 ,//0x0F��������
 0x00 ,//0x10��������
 0x14 ,//0x11������+��
 0x15 ,//0x12������-��
 0x12 ,//0x13��Ƶ��+��
 0x11 ,//0x14��Ƶ��-��
 0x57 ,//0x15���ϣ�
 0x20 ,//0x16���£�
 0xD7 ,//0x17����
 0xD8 ,//0x18���ң�
 0x52 ,//0x19��ȷ����
 0x00 ,//0x1A���ź�Դ��
 0x6B ,//0x1B����ҳ��
 0x00 ,//0x1C��������
 0x00 ,//0x1D��������
 0x00 ,//0x1E������0��
 0x00 ,//0x1F������1��
 0x00 ,//0x20������2��
 0x00 ,//0x21������3��
 0x00 ,//0x22������4��
 0x00 ,//0x23������5��
 0x00 ,//0x24������6��
 0x00 ,//0x25������7��
 0x00 ,//0x26������8��
 0x00 ,//0x27������9��
 0x00 ,//0x28�����뷨��
};

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t BoxSendlen = 0;

/* Private function prototypes ---------------------------------------------*/
static void PackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void PackageByte(uint8_t byte) {
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x01<<(i))) {	//�ߵ�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		} else {				//�͵�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void TVBox89IrPackage(void *p) {
	uint8_t  i,p_byte = 0x00, flag=0;

	uint8_t codeNum = *(uint8_t*)p;

	if(codeNum == 0 || codeNum >= TVBOX_KEY_NUM) return ;
	
	p_byte = BOX_Code[codeNum-1];

	if(p_byte == 0x00) return;
	
	//�ȴ��������
	//while(IrDev.State != IRREMOTE_IDLE);  
	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));
	
	BoxSendlen = 0;

	for(i=0;i<3;i++) {
		//����������
		IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		
		if(p_byte != TV_BOX_ONOFF && p_byte != TV_BOX_MENU && p_byte != TV_BOX_RESERVED6){
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
		else {
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		}
		
		//���ÿ�����
		if(flag&0x01) PackageByte(~p_byte);
		else PackageByte(p_byte);
	
		//����ֹͣ��
		if(flag&0x01) {
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		}
		else{
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
		
		//���ý�����
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
		
		flag++;
	}
	
	//�������ύ
	IrDev.Ptr 		= IrSendBuf;
	IrDev.Len 		= BoxSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
